package main

import (
  "net"
)

type User struct {
  Name string 
  Addr string
  C chan string
  conn net.Conn

  //
  server *Server
}

//api:creat
func NewUser(conn net.Conn, server *Server) *User {
  userAddr := conn.RemoteAddr().String()

  user := &User{
    Name: userAddr,
    Addr: userAddr,
    C: make(chan string),
    conn: conn,

    server: server,
  }

  // 
  go user.ListenMessage()

  return user
}

//listen User channel
func (this *User) ListenMessage() {
  for {
    msg := <-this.C

    this.conn.Write([]byte(msg + "\n"))
  }
}

func (this *User) Online(){
  // add user to online map 
  this.server.mapLock.Lock()
  this.server.OnlineMap[this.Name] = this
  this.server.mapLock.Unlock()

  // broadcast this user has online
  this.server.Broadcast(this, "online.")

}
func (this *User) Offline(){
  // add user to online map 
  this.server.mapLock.Lock()
  delete( this.server.OnlineMap, this.Name)
  this.server.mapLock.Unlock()

  // broadcast this user has online
  this.server.Broadcast(this, "Offline.")

}
func (this *User) DoMessage(msg string){
  this.server.Broadcast(this, msg)
}
